﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Facebook.Schema;
using Google.YouTube;
using log4net;

namespace Social
{

	public class SocialNetworksService
	{
		private static ILog logger = LogManager.GetLogger(typeof(SocialNetworksService));

		#region Const Variables
		public const string URL_REGEX_PATTERN = @"(((?<Protocol>\w+):\/\/)|www.)(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*(\#[^\s]*)?";
		public static readonly string ebApiKey = ConfigurationManager.AppSettings["eventbriteAppKey"];
		#endregion

		private string accessToken;

		private SocialNetwork socialNetwork;

		public SocialNetworksService(string accessToken, SocialNetwork socialNetwork)
		{
			this.accessToken = accessToken;
			this.socialNetwork = socialNetwork;
		}

		#region Publishing Methods
		public void PublishVideo(string file)
        {
			socialNetwork.UploadVideo(file);
        }

		public void UploadPhoto(string file)
		{
			socialNetwork.UploadPhoto(file);
		}

		//private static void SetFacebookPostId(BO.PostedItem item, SocialNetworksServiceResult result, string fbPostID)
		//{
		//    if (!string.IsNullOrEmpty(fbPostID))
		//    {
		//        item.FacebookFeedItemID = fbPostID;
		//        result.FacebookSuccess = SocialNetworksServiceResult.ResultState.Success;
		//    }
		//    else result.FacebookSuccess = SocialNetworksServiceResult.ResultState.Failure;
		//}

		//public static void PublishOnTwitter(BO.PostedItem item, SocialNetworksServiceResult result, BO.User currentUser, string tweet)
		//{
		//    var twitterClient = SocialNetworksAuthenticator.GetTwitterClient(currentUser);
		//    if (twitterClient == null)
		//    {
		//        result.TwitterSuccess = SocialNetworksServiceResult.ResultState.Failure;
		//        return;
		//    }
		//    var updateResult = twitterClient.Post(tweet);

		//    if (updateResult.ResultOption == ServiceResultOptions.Success)
		//    {
		//        item.TwitterStatusID = updateResult.Content.Id.ToString();
		//        result.TwitterSuccess = SocialNetworksServiceResult.ResultState.Success;
		//    }
		//    else result.FacebookSuccess = SocialNetworksServiceResult.ResultState.Failure;
            
		//}
#endregion

	}

	public class SocialNetworksServiceResult
    {
		#region Properties
        public ResultState FacebookSuccess { get; set; }
        public ResultState TwitterSuccess { get; set; }
		#endregion

		#region Methods
		public string RenderResult()
        {
            StringBuilder sb = new StringBuilder("the content ");
            sb.Append(GetResultStringForNetwork("Facebook", this.FacebookSuccess));
            sb.Append(GetResultStringForNetwork("Twitter", this.TwitterSuccess));
            
           
            return sb.ToString();
        }

        private string GetResultStringForNetwork(string networkName, ResultState resultState)
        {
            switch (resultState)
            {
                case ResultState.Success: return string.Format("was published On {0}. ", networkName);
                case ResultState.Failure: return string.Format("was not published On {0}. ", networkName);
                default:
                    break;
            }
            return string.Empty;
		}
		#endregion

		#region Enums

		public enum ResultState
        { 
            None,
            Success,
            Failure
		}
		#endregion
	}

	//public class SocialComment
	//{
	//    #region Properties
	//    public string Responder { get; set; }
	//    public string Text { get; set; }
	//    public DateTime Published { get; set; }
	//    public CommentType SocialNetwork {get; set; }
	//    #endregion

	//    #region Constructor
	//    public SocialComment(string responder,string text,DateTime published, CommentType commentSource)
	//    {
	//        this.Responder = responder;
	//        this.Text = text;
	//        this.Published = published;
	//        this.SocialNetwork = commentSource;
	//    }
	//    #endregion
	//}

	public enum CommentType
	{
		facebook = 1,
		twitterRT = 2,
		twitterREP =3,
		youtube = 4,
		eventbrite = 5,
		demoscratiBlog = 6,
		flickr = 7, 
		wordpress =8
	}
}
